package com.smarthealthring.mm.ui.fragment;

import android.content.Intent;
import android.media.Image;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseFragment;
import com.smarthealthring.mm.base.IPresenter;
import com.smarthealthring.mm.clj.blesample.FastBleMainActivity;
import com.smarthealthring.mm.ui.activity.BindAccountActivity;
import com.smarthealthring.mm.ui.activity.BondIdentityActivity;
import com.smarthealthring.mm.ui.activity.DeviceInfoActivity;
import com.smarthealthring.mm.ui.activity.DevicesActivity;
import com.smarthealthring.mm.ui.activity.PersonInfoActivity;
import com.smarthealthring.mm.ui.activity.QAActivity;
import com.smarthealthring.mm.ui.activity.SearchBtActivity;
import com.smarthealthring.mm.ui.activity.SettingActivity;
import com.smarthealthring.mm.ui.activity.SportTagetActivity;
import com.smarthealthring.mm.ui.activity.WeightTagetActivity;
import com.smarthealthring.mm.utils.GlideApp;
import com.smarthealthring.mm.utils.SPUtils;
import com.smarthealthring.mm.utils.ToastUtil;
import com.smarthealthring.mm.utils.statusbar.StatusBarCompat;
import com.smarthealthring.mm.widgets.RoundImageView;
import com.smarthealthring.mm.widgets.SingleItemView;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Created by hewei26 on 2018/4/8.
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.item_fastble)
    SingleItemView mItemFastBle;
    @BindView(R.id.siv_device)
    SingleItemView mItemMagciRing;
    @BindView(R.id.siv_sport_target)
    SingleItemView mItemSportTarget;
    @BindView(R.id.siv_weight_target)
    SingleItemView mItemWeightTarget;
    @BindView(R.id.siv_setting)
    SingleItemView mItemSetting;
    @BindView(R.id.siv_qa)
    SingleItemView mItemQa;
    @BindView(R.id.iv_head_photo)
    RoundImageView mImgHeadPhoto;
    @BindView(R.id.siv_bind_account) //账号绑定
            SingleItemView sivBindAccount;
    @BindView(R.id.tv_conn_new) //账号绑定
            TextView mTvConnNew;


    @Override
    protected int getLayoutId() {
        return R.layout.frag_mine;
    }

    @Override
    protected void initEventAndData() {
        StatusBarCompat.setStatusBarColor(mActivity, ContextCompat.getColor(mActivity, R.color.title_blue2));
        mItemFastBle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, FastBleMainActivity.class);
                startActivity(intent);
            }
        });
        mItemMagciRing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SPUtils.getInt(SPUtils.BOND_STATE, 0) == 1 &&
                        BleManager.getInstance().isConnected(SPUtils.getString(SPUtils.DEVICE_MAC, ""))) {
                    Intent intent = new Intent(mActivity, DeviceInfoActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.longShow("设备未连接");
                }

            }
        });
        mImgHeadPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, PersonInfoActivity.class);
                startActivity(intent);
            }
        });
        mItemSportTarget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, SportTagetActivity.class);
                startActivity(intent);
            }
        });
        mItemWeightTarget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, WeightTagetActivity.class);
                startActivity(intent);
            }
        });
        mItemSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, SettingActivity.class);
                startActivity(intent);
            }
        });

        mItemQa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, QAActivity.class);
                startActivity(intent);
            }
        });

        //账号绑定
        RxView.clicks(sivBindAccount)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivity(new Intent(mContext, BondIdentityActivity.class));
                    }
                });
        mTvConnNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, DevicesActivity.class));
            }
        });
        mItemMagciRing.setmTvRight("剩余电量："+SPUtils.getInt(SPUtils.DEVICE_POWER, 75) + "%");
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    @Override
    protected boolean isLazyLoad() {
        return false;
    }

    public static MineFragment getInstance() {
        return new MineFragment();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            StatusBarCompat.setStatusBarColor(mActivity, ContextCompat.getColor(mActivity, R.color.title_blue2));
        }
        GlideApp.with(mContext).load(SPUtils.getString(SPUtils.USER_PHOTO_URL, "")).into(mImgHeadPhoto);
    }

    @Override
    public void onResume() {
        super.onResume();
        GlideApp.with(mContext).load(SPUtils.getString(SPUtils.USER_PHOTO_URL, "")).into(mImgHeadPhoto);
    }
}
